package com.dm0858.bianmin.mine.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.dm0858.bianmin.R;
import com.dm0858.bianmin.databinding.ActivityMyFavoriteLayoutBinding;
import com.dm0858.bianmin.mine.adapter.MyFavoriteRecyclerViewAdapter;
import com.dm0858.bianmin.mine.base.BaseActivity;
import com.dm0858.bianmin.mine.bean.MyFavoriteBeanData;
import com.dm0858.bianmin.mine.tools.LogUtils;
import com.dm0858.bianmin.mine.viewmodel.MineViewModel;
import com.dm0858.bianmin.ui.activity.ShangJiaActivity;

import java.util.List;

/**
 * @description 我的收藏
 * @author 迪迪丶Sir
 * @e-mail asd969709719@qq.com
 * @since 2020-05-02
 **/
public class MyFavoriteActivity extends BaseActivity implements View.OnClickListener {

    private ActivityMyFavoriteLayoutBinding binding;

    private MineViewModel viewModel;

    private MyFavoriteRecyclerViewAdapter adapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this,R.layout.activity_my_favorite_layout);
        binding.setOnClick(this);
        initData();
    }

    private List<MyFavoriteBeanData.DataBean> dataBeans;

    @Override
    protected void initData() {
        viewModel = new ViewModelProvider(this).get(MineViewModel.class);
        viewModel.userCollectArticle();
        viewModel.getMyFavoriteList().observe(this, dataBeans -> {
            if (dataBeans != null)
                this.dataBeans = dataBeans;
            if (adapter == null) {
                adapter = new MyFavoriteRecyclerViewAdapter(this,R.layout.adapter_my_favorite_item_layout,dataBeans);
                binding.recyclerview.setLayoutManager(new GridLayoutManager(this,1));
                binding.recyclerview.setAdapter(adapter);
            }else {
                adapter.setList(dataBeans);
            }
            adapter.setOnItemClick((data, position) -> {
                Intent intent = new Intent(this, ShangJiaActivity.class);
                intent.putExtra("0",data.getId());
                intent.putExtra("lat",data.getLat());
                intent.putExtra("lng",data.getLng());
                intent.putExtra("addr",data.getAddr());
                intent.putExtra("position",position);
                startActivityForResult(intent,1000);
            });
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                onBackPressed();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000) {
            LogUtils.d(" " + resultCode);
            if (resultCode == 1001){
                if (data != null) {
                    int position = data.getIntExtra("position",-1);
                    if (position == -1) return;
                    else {
                        dataBeans.remove(position);
                        adapter.setList(dataBeans);
                    }
                }
            }
        }
    }
}